package com.ruoyi.weixin.mp.entity.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.ruoyi.weixin.mp.utils.Json;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author anan.
 * @version 1.0
 * @create 2021-05-30 15-13
 */
@Data
public class WxUserVo {

    private static final long serialVersionUID = 1L;

    @TableId(type = IdType.INPUT)
    private String openid;

    private String appid;

    private String phone;

    private String nickname;

    private int sex;

    private String city;

    private String province;

    private String headimgurl;

    @JSONField(name = "subscribe_time")
    private Date subscribeTime;

    private boolean subscribe;

    private String unionid;

    private String remark;

    private JSONArray tagidList;

    private String subscribeScene;

    private String qrSceneStr;

    public WxUserVo() {
    }

    public WxUserVo(String openid) {
        this.openid = openid;
    }

    public WxUserVo(WxMpUser wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenId();
        this.appid = appid;
        this.subscribe=wxMpUser.getSubscribe();
        if(wxMpUser.getSubscribe()){
            this.nickname = wxMpUser.getNickname();
            this.sex = wxMpUser.getSex();
            this.city = wxMpUser.getCity();
            this.province = wxMpUser.getProvince();
            this.headimgurl = wxMpUser.getHeadImgUrl();
            this.subscribeTime = new Date(wxMpUser.getSubscribeTime()*1000);
            this.unionid=wxMpUser.getUnionId();
            this.remark=wxMpUser.getRemark();
            this.tagidList=JSONArray.parseArray(JSONObject.toJSONString(wxMpUser.getTagIds()));
            this.subscribeScene=wxMpUser.getSubscribeScene();
            String qrScene =  wxMpUser.getQrScene();
            this.qrSceneStr= !StringUtils.hasText(qrScene) ? wxMpUser.getQrSceneStr() : qrScene;
        }
    }

    public WxUserVo(WxOAuth2UserInfo wxMpUser, String appid) {
        this.openid = wxMpUser.getOpenid();
        this.appid = appid;
        this.subscribe=wxMpUser.getNickname()!=null;
        if(this.subscribe){
            this.nickname = wxMpUser.getNickname();
            this.sex = wxMpUser.getSex();
            this.city = wxMpUser.getCity();
            this.province = wxMpUser.getProvince();
            this.headimgurl = wxMpUser.getHeadImgUrl();
            this.unionid=wxMpUser.getUnionId();
        }
    }

    @Override
    public String toString() {
        return Json.toJsonString(this);
    }
}
